<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 公司逻辑
 */
class Company extends AdminBase
{

    /**
     * 获取公司列表
     */
    public function getCompanyList($where = [], $field = true, $order = 'create_time desc', $paginate = false)
    {
        
        return $this->modelCompany->getList($where, $field, $order, $paginate);
    }
    
    
    /**
     * 公司添加
     */
    public function companyAdd($data = [])
    {
        
        $validate_result = $this->validateCompany->scene('add')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateCompany->getError()];
        }
        
        $url = url('companyList');
        
        // $data['nickname']  = $data['username'];
        // $data['leader_id'] = MEMBER_ID;
        // $data['is_inside'] = DATA_NORMAL;
        
        $result = $this->modelCompany->setInfo($data);
        
        $result && action_log('新增', '新增公司，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '公司添加成功', $url] : [RESULT_ERROR, $this->modelCompany->getError()];
    }
    
    /**
     * 获取公司信息
     */
    public function getCompanyInfo($where = [])
    {
        
        return $this->modelCompany->getInfo($where);
    }

    /**
     * 管理员编辑
     */
    public function companyEdit($data = [])
    {
        
        $validate_result = $this->validateCompany->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateCompany->getError()];
        }
        
        $url = url('companyList');
        
        $result = $this->modelCompany->setInfo($data);
        
        $result && action_log('编辑', '编辑公司，id：' . $data['id']);
        
        return $result ? [RESULT_SUCCESS, '公司编辑成功', $url] : [RESULT_ERROR, $this->modelCompany->getError()];
    }
    
    /**
     * 设置管理员信息
     */
    public function setMemberValue($where = [], $field = '', $value = '')
    {
        
        return $this->modelMember->setFieldValue($where, $field, $value);
    }
    
    /**
     * 管理员删除
     */
    public function companyDel($where = [])
    {
        
        $url = url('companyList');
        
        if (SYS_ADMINISTRATOR_ID == $where['id'] || MEMBER_ID == $where['id']) {
            
            return [RESULT_ERROR, '天神和自己不能删除哦~', $url];
        }
        
        $result = $this->modelCompany->deleteInfo($where);
                
        $result && action_log('删除', '删除公司，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '公司删除成功', $url] : [RESULT_ERROR, $this->modelCompany->getError(), $url];
    }
}
